import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/reports.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/grids/basketDataGrid.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';

import '../../constants.dart';
import '../../themes/appTheme.dart';

enum ContentOptions { RECORDING, WRITING }

enum StatusOptions { NEW, REVIEWED, GRADED }

// ignore: must_be_immutable
class TeacherReportsBasket extends StatefulWidget {
  TeacherReportsBasket({Key? key, required this.showAppbar, this.filter})
      : super(key: key);

  static const String routeName = '/teacher/reports/basket';
  bool showAppbar = false;
  String? filter;

  @override
  _TeacherReportsBasket createState() => _TeacherReportsBasket();
}

class _TeacherReportsBasket extends State<TeacherReportsBasket> {
  final ReportsController _reportsController = Get.put(ReportsController());
  final box = Hive.box('appconfig');

  List<String> content = [];
  List<String> status = [];
  String classId = '';

  @override
  void initState() {
    classId = box.get(CacheManagerKey['currentclass'].toString());
    if (_reportsController.basketItems.isNotEmpty) {
      _reportsController.basketItems.clear();
    }
    if (widget.filter == 'RECORDING') {
      content.add('RECORDING');
      //status.add('NEW');
    } else if (widget.filter == 'WRITING') {
      content.add('WRITING');
      //status.add('NEW');
    }
    Future.delayed(Duration(milliseconds: 200), (() {
      if (classId != '') {
        fetchData(classId, content, status);
      }
    }));
    super.initState();
  }

  void fetchData(classId, content, status) {
    Log.debug(classId);
    _reportsController.fetchTeacherBaskets(classId, content, status);
  }

  @override
  void dispose() {
    _reportsController.basketItems.clear();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build teacher report Basket');
    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    return Scaffold(
        backgroundColor: Colors.transparent,
        appBar:
            widget.showAppbar ? CustomAppBar(title: 'Basket Report'.tr) : null,
        body: Column(
          children: [
            Padding(
              padding: const EdgeInsets.symmetric(vertical: 9.0),
              child: Row(
                mainAxisAlignment: isSmallScreen
                    ? MainAxisAlignment.center
                    : MainAxisAlignment.end,
                children: [
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 4.5),
                    child: ChoiceChip(
                      label: Text(
                        "Recording",
                        style: TextStyle(color: AppTheme.colors.lightOrange),
                      ),
                      onSelected: (value) {
                        List<String> temp = content;
                        if (!value)
                          temp.remove("RECORDING");
                        else
                          temp.add("RECORDING");
                        setState(() {
                          content = temp;
                        });
                        fetchData(classId, temp, status);
                      },
                      selected: content.contains("RECORDING"),
                      backgroundColor: Colors.grey,
                      selectedColor: AppTheme.colors.orangeBtn,
                      labelPadding: EdgeInsets.symmetric(horizontal: 12.0),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 4.5),
                    child: ChoiceChip(
                      label: Text(
                        "Writing",
                        style: TextStyle(color: AppTheme.colors.lightOrange),
                      ),
                      onSelected: (value) {
                        List<String> temp = content;
                        if (!value)
                          temp.remove("WRITING");
                        else
                          temp.add("WRITING");
                        setState(() {
                          content = temp;
                        });
                        fetchData(classId, temp, status);
                      },
                      selected: content.contains("WRITING"),
                      backgroundColor: Colors.grey,
                      selectedColor: AppTheme.colors.orangeBtn,
                      labelPadding: EdgeInsets.symmetric(horizontal: 12.0),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 4.5),
                    child: ChoiceChip(
                      label: Text(
                        "New",
                        style: TextStyle(color: AppTheme.colors.lightOrange),
                      ),
                      onSelected: (value) {
                        List<String> temp = status;
                        if (!value)
                          temp.remove("NEW");
                        else
                          temp.add("NEW");
                        setState(() {
                          status = temp;
                        });
                        fetchData(classId, content, temp);
                      },
                      selected: status.contains("NEW"),
                      backgroundColor: Colors.grey,
                      selectedColor: AppTheme.colors.orangeBtn,
                      labelPadding: EdgeInsets.symmetric(horizontal: 12.0),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 4.5),
                    child: ChoiceChip(
                      label: Text(
                        "Reviewed",
                        style: TextStyle(color: AppTheme.colors.lightOrange),
                      ),
                      onSelected: (value) {
                        List<String> temp = status;
                        if (!value)
                          temp.remove("REVIEWED");
                        else
                          temp.add("REVIEWED");
                        setState(() {
                          status = temp;
                        });
                        fetchData(classId, content, temp);
                      },
                      selected: status.contains("REVIEWED"),
                      backgroundColor: Colors.grey,
                      selectedColor: AppTheme.colors.orangeBtn,
                      labelPadding: EdgeInsets.symmetric(horizontal: 12.0),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(horizontal: 4.5),
                    child: ChoiceChip(
                      label: Text(
                        "Graded",
                        style: TextStyle(color: AppTheme.colors.lightOrange),
                      ),
                      onSelected: (value) {
                        List<String> temp = status;
                        if (!value)
                          temp.remove("GRADED");
                        else
                          temp.add("GRADED");
                        setState(() {
                          status = temp;
                        });
                        fetchData(classId, content, temp);
                      },
                      selected: status.contains("GRADED"),
                      backgroundColor: Colors.grey,
                      selectedColor: AppTheme.colors.orangeBtn,
                      labelPadding: EdgeInsets.symmetric(horizontal: 12.0),
                    ),
                  ),

                  // Flexible(
                  //   child: Container(
                  //       width: 130,
                  //       child: LabeledCheckbox(
                  //         label: "Recording",
                  //         value: content.contains("RECORDING"),
                  //         onChanged: (value) {
                  //           List<String> temp = content;
                  //           if (!value)
                  //             temp.remove("RECORDING");
                  //           else
                  //             temp.add("RECORDING");
                  //           setState(() {
                  //             content = temp;
                  //           });
                  //           fetchData(classId, temp, status);
                  //         },
                  //         padding: const EdgeInsets.all(1.0),
                  //       )),
                  // ),
                  // Flexible(
                  //   child: Container(
                  //       width: 130,
                  //       child: LabeledCheckbox(
                  //         label: "Writing",
                  //         value: content.contains("WRITING"),
                  //         onChanged: (value) {
                  //           List<String> temp = content;
                  //           if (!value)
                  //             temp.remove("WRITING");
                  //           else
                  //             temp.add("WRITING");
                  //           setState(() {
                  //             content = temp;
                  //           });
                  //           fetchData(classId, temp, status);
                  //         },
                  //         padding: const EdgeInsets.all(1.0),
                  //       )),
                  // ),
                  // Flexible(
                  //   child: Container(
                  //       width: 130,
                  //       child: LabeledCheckbox(
                  //         label: "New",
                  //         value: status.contains("NEW"),
                  //         onChanged: (value) {
                  //           List<String> temp = status;
                  //           if (!value)
                  //             temp.remove("NEW");
                  //           else
                  //             temp.add("NEW");
                  //           setState(() {
                  //             status = temp;
                  //           });
                  //           fetchData(classId, content, temp);
                  //         },
                  //         padding: const EdgeInsets.all(1.0),
                  //       )),
                  // ),
                  // Flexible(
                  //   child: Container(
                  //       width: 130,
                  //       child: LabeledCheckbox(
                  //         label: "Reviewed",
                  //         value: status.contains("REVIEWED"),
                  //         onChanged: (value) {
                  //           List<String> temp = status;
                  //           if (!value)
                  //             temp.remove("REVIEWED");
                  //           else
                  //             temp.add("REVIEWED");
                  //           setState(() {
                  //             status = temp;
                  //           });
                  //           fetchData(classId, content, temp);
                  //         },
                  //         padding: const EdgeInsets.all(1.0),
                  //       )),
                  // ),
                  // Flexible(
                  //   child: Container(
                  //       width: 130,
                  //       child: LabeledCheckbox(
                  //         label: "Graded",
                  //         value: status.contains("GRADED"),
                  //         onChanged: (value) {
                  //           List<String> temp = status;
                  //           if (!value)
                  //             temp.remove("GRADED");
                  //           else
                  //             temp.add("GRADED");
                  //           setState(() {
                  //             status = temp;
                  //           });
                  //           fetchData(classId, content, temp);
                  //         },
                  //         padding: const EdgeInsets.all(1.0),
                  //       )),
                  // ),
                ],
              ),
            ),
            _reportsController.obx(
              (data) => Container(
                child: BasketDataGrid(
                  basketItems: _reportsController.basketItems,
                ),
              ),
              onEmpty: Container(
                  child: BasketDataGrid(
                basketItems: _reportsController.basketItems,
              )),
              onLoading: Container(),
            )
          ],
        ));
  }
}
